#include "OQLPronosticResult.h"

OQLPronosticResult::OQLPronosticResult()
{
}

OQLPronosticResult::~OQLPronosticResult()
{
}

ListPersistent OQLPronosticResult::resolve(std::string tabla){
	
	ListPersistent list;
	ResolveDBRequest res;
	
	double minAltm = 10000;
	double maxAltm = -1000;
	double minRh = 1000;
	double maxRh = -1000;
	double minTmp = 1000;
	double maxTmp = -1000;
	
	bool isValid = false;
	
	OQLSimpleCondition simpleStation(SampleSourcesafe::CODE, "=", stationCode);
	simpleStation.setTable(tabla);
	
	ListPersistent lsSimple = res.resolveSimple(simpleStation);
	
	for(int i = 0; i < lsSimple.getSize(); i++){
		
		Persistent * current = lsSimple.getPersistent(i);
		
		isValid = true;
		
		std::string altm;
		if( (altm = current->getValor("altm"))!= "" && NumberUtil::isNumber(altm)){
			double aux = atof(altm.c_str());
			if (aux > maxAltm){
				maxAltm = aux;
			}
			if (aux < minAltm){
				minAltm = aux;
			}
		}
		
		std::string tmp;
		if( (tmp = current->getValor("tmp"))!= "" && NumberUtil::isNumber(tmp)){
			double aux = atof(tmp.c_str());
			if (aux > maxTmp){
				maxTmp = aux;
			}
			if (aux < minTmp){
				minTmp = aux;
			}
			
		}
		
		std::string rh;
		if( (rh = current->getValor("rh"))!= "" && NumberUtil::isNumber(rh)){
			double aux = atof(rh.c_str());
			if (aux > maxRh){
				maxRh = aux;
			}
			if (aux < minRh){
				minRh = aux;
			}
			
		}
		
	}
	
	if (isValid){
		
		ResultPronostic *result = new ResultPronostic();
		
		result->setMaxAltm(maxAltm);
		result->setMinAltm(minAltm);
		result->setMaxRh(maxRh);
		result->setMinRh(minRh);
		result->setMaxTmp(maxTmp);
		result->setMinTmp(minTmp);
		
		list.insertSort((Persistent*)result);
		
	}
	
	return list;
}

void OQLPronosticResult::erase()
{
	// No hace absolutamente nada
}
